/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import edu.soc.cs5226.gui.entities.util.Constants;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class RollbackSegmentProcessList {
    public dayObject processTreeNode(Timestamp now, List<xObject> xList, HealthStatus hs)
    {
        long total24Number = 0;
        long total24Waits = 0;
        long total24Gets = 0;
        long total24RollbackSegment = 0;
        
        Iterator<xObject> xIter = xList.iterator();
        while (xIter.hasNext())
        {
            long totalNumber = 0;
            long totalWaits = 0;
            long totalGets = 0;
            long totalRollbackSegment = 0;
            xObject currX = xIter.next();
            Iterator<yObject> yIter = currX.getyObjects().iterator();
            while (yIter.hasNext())
            {
                long subtotalNumber = 0;
                long subtotalWaits = 0;
                long subtotalGets = 0;
                long subtotalRollbackSegment = 0;
                yObject currY = yIter.next();
                Iterator<DBParameter> rsIter = currY.getObj().iterator();
                while (rsIter.hasNext())
                {
                    RollbackSegment rsObj = (RollbackSegment)rsIter.next();
                    total24Waits = total24Waits + rsObj.getWaits();
                    total24Gets = total24Gets + rsObj.getGets();
                    total24RollbackSegment = total24RollbackSegment + rsObj.getRollbackSegment();
                    total24Number++;
                    
                    totalWaits = totalWaits + rsObj.getWaits();
                    totalGets = totalGets + rsObj.getGets();
                    totalRollbackSegment = totalRollbackSegment + rsObj.getRollbackSegment();
                    totalNumber++;
                    
                    subtotalWaits = subtotalWaits + rsObj.getWaits();
                    subtotalGets = subtotalGets + rsObj.getGets();
                    subtotalRollbackSegment = subtotalRollbackSegment + rsObj.getRollbackSegment();
                    subtotalNumber++;
                }
                RollbackSegment treenodeY = (RollbackSegment)currY.getyObj();
                treenodeY.setWaits(calcAverage(subtotalWaits,subtotalNumber));
                treenodeY.setGets(calcAverage(subtotalGets,subtotalNumber));
                treenodeY.setRollbackSegment(calcAverage(subtotalRollbackSegment,subtotalNumber));
                treenodeY.setRs_healthStatus(processRollbackSegment(treenodeY, hs));
            }
            RollbackSegment treenodeX = (RollbackSegment)currX.getxObj();
            treenodeX.setWaits(calcAverage(totalWaits,totalNumber));
            treenodeX.setGets(calcAverage(totalGets,totalNumber));
            treenodeX.setRollbackSegment(calcAverage(totalRollbackSegment,totalNumber));
            treenodeX.setRs_healthStatus(processRollbackSegment(treenodeX, hs));
        }
        
        RollbackSegment treenodeRoot = (RollbackSegment)DBParamFactory.getDBParameter(Constants.ROLLBACKSEGMENT);
        treenodeRoot.setWaits(calcAverage(total24Waits,total24Number));
        treenodeRoot.setGets(calcAverage(total24Gets,total24Number));
        treenodeRoot.setRollbackSegment(calcAverage(total24RollbackSegment,total24Number));
        treenodeRoot.setRs_healthStatus(processRollbackSegment(treenodeRoot, hs));
        treenodeRoot.setdateTime(new Timestamp(now.getTime()-(24*60*60*1000)));
        
        dayObject dObject = new dayObject();
        dObject.setRootObj(treenodeRoot);
        dObject.setxObjects(xList);
        
        return dObject;
    }
    
    //--------------------------------------------------------------------------
    
    private long calcAverage(long value, long number)
    {
        if (value>0)
            return (value/number);
        else
            return 0;
    }
    
    //--------------------------------------------------------------------------
    // processRollbackSegments obtains rollback segments info from the database
    // and calculcates the status indicator
    // if the rollbackSegment < yellow indicator
    //    status is green.
    // if the rollbackSegment >= yellow & rollbackSegment < red indicator
    //    status is yellow
    // if the rollbackSegment >= red indicator
    //    status is red
    //--------------------------------------------------------------------------
    private String processRollbackSegment(RollbackSegment rs, HealthStatus hs)
    {
        String status = "G";
        if ((rs.getRollbackSegment()>=hs.getyellowThres()) && 
                (rs.getRollbackSegment()<hs.getredThres()))
        {
            status = "Y";
        }
        else if (rs.getRollbackSegment()>=hs.getredThres())
        {
            status = "R";
        }
        return status;
    } // processRollbackSegment
}
